from django.shortcuts import render
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status  # 状态码常量模块
from rest_framework.renderers import JSONRenderer
from app01.models import Book, Publish
from app01.ser import BookSerializer, PublishSerializer
from rest_framework.generics import GenericAPIView


# Create your views here.


class TestView(APIView):
    renderer_classes = [JSONRenderer, ]  # 局部配置 响应模式

    def get(self, request):
        print(request, type(request))

        return Response(data={'name': 'AK'}, status=status.HTTP_201_CREATED, headers={'token': 'xx'})


# 基于APIView写的
class TestView2(APIView):
    # renderer_classes = [JSONRenderer, ]  # 局部配置 响应模式

    def get(self, request):
        print(request, type(request))

        return Response(data={'name': 'AK'}, status=status.HTTP_201_CREATED, headers={'token': 'xxxx'})


class BookView(APIView):
    def get(self, request):
        book_list = Book.objects.all()
        book_ser = BookSerializer(instance=book_list, many=True)
        return Response(book_ser.data)

    def post(self, request):
        book_ser = BookSerializer(data=request.data)
        if book_ser.is_valid():
            book_ser.save()
            return Response(book_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})


class BookDetailView(APIView):

    def get(self, request, pk):
        book = Book.objects.all().filter(pk=pk).first()
        book_ser = BookSerializer(instance=book)
        return Response(book_ser.data)

    def put(self, request, pk):

        book = Book.objects.filter(pk=pk).first()
        book_ser = BookSerializer(data=request.data, instance=book)
        if book_ser.is_valid():
            book_ser.save()
            return Response(book_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})

    def delete(self, request, pk):
        ret = Book.objects.filter(pk=pk).delete()
        return Response({'status': 100, 'msg': '删除成功'})


# 基于GenericAPIView写的
class Book2View(GenericAPIView):
    # queryset = Book.objects.all()
    queryset = Book.objects
    print(queryset)
    serializer_class = BookSerializer
    def get(self, request):
        book_list = self.get_queryset()
        book_ser = self.get_serializer(book_list, many=True)
        return Response(book_ser.data)

    def post(self, request):
        book_ser = self.get_serializer(data=request.data)
        if book_ser.is_valid():
            book_ser.save()
            return Response(book_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})


class Book2DetailView(GenericAPIView):
    queryset = Book.objects
    serializer_class = BookSerializer
    def get(self, request, pk):
        book = self.get_object()
        book_ser = self.get_serializer(book)
        return Response(book_ser.data)

    def put(self, request, pk):
        book = self.get_object()
        book_ser = self.get_serializer(data=request.data)
        if book_ser.is_valid():
            book_ser.save()
            return Response(book_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})

    def delete(self, request, pk):
        ret = self.get_object().delete()
        return Response({'status': 100, 'msg': '删除成功'})


class PublishView(GenericAPIView):
    queryset = Publish.objects
    serializer_class = PublishSerializer

    def get(self, request):
        publish_list = self.get_queryset()
        publish_ser = self.get_serializer(instance=publish_list, many=True)
        print(publish_ser.data)
        return Response(publish_ser.data)

    def post(self, request):
        publish_ser = self.get_serializer(data=request.data)
        if publish_ser.is_valid():
            publish_ser.save()
            print(publish_ser.data)
            return Response(publish_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})


class PublishDetailView(GenericAPIView):
    queryset = Publish.objects
    serializer_class = PublishSerializer

    def get(self, request, pk):
        publish = self.get_object()
        publish_ser = self.get_serializer(instance=publish)
        return Response(publish_ser.data)

    def put(self, request, pk):
        publish = self.get_object()
        publish_ser = self.get_serializer(data=request.data, instance=publish)
        if publish_ser.is_valid():
            publish_ser.save()
            return Response(publish_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})

    def delete(self, request, pk):
        ret = self.get_object().delete()
        return Response({'status': 100, 'msg': '删除成功'})


class Publish2View(APIView):
    def get(self, request):
        publish = Publish.objects.all()
        publish_ser = PublishSerializer(instance=publish, many=True)
        return Response(publish_ser.data)

    def post(self, request):
        publish_ser = PublishSerializer(data=request.data)
        if publish_ser.is_valid():
            publish_ser.save()
            return Response(publish_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})


class Publish2DetailView(APIView):
    def get(self, request, pk):  # pk 其实是 pk=pk kwargs
        publish = Publish.objects.filter(id=pk).first()
        publish_ser = PublishSerializer(instance=publish)
        return Response(publish_ser.data)

    def put(self, request, pk):
        publish = Publish.objects.filter(id=pk).first()
        publish_ser = PublishSerializer(data=request.data, instance=publish)
        if publish_ser.is_valid():
            publish_ser.save()
            return Response(publish_ser.data)
        else:
            return Response({'status': 101, 'msg': '校验失败'})

    def delete(self, request, pk):
        ret = Publish.objects.filter(pk=pk).delete()
        return Response({'status': 100, 'msg': '删除成功'})


# 5个视图扩展类
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, DestroyModelMixin, UpdateModelMixin


class Book3View(GenericAPIView, ListModelMixin, CreateModelMixin):

    queryset = Book.objects
    serializer_class = BookSerializer

    def get(self, request):
        return self.list(request=request)

    def post(self, request):
        return self.create(request=request)


class Book3DetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
    queryset = Book.objects
    serializer_class = BookSerializer

    def get(self, request, pk):
        return self.retrieve(request=request, pk=pk)

    def put(self, request, pk):
        return self.update(request=request, pk=pk)

    def delete(self, request, pk):
        return self.destroy(request, pk=pk)


# GenericAPIView的9个视图子类
from rest_framework.generics import CreateAPIView, ListAPIView, UpdateAPIView, RetrieveAPIView, DestroyAPIView
from rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView, RetrieveDestroyAPIView, RetrieveUpdateAPIView


# class Book4View(ListAPIView, CreateAPIView):  # 获取所有, 新增一个 相当于继承 ListCreateAPIView
class Book4View(ListCreateAPIView):
    queryset = Book.objects
    serializer_class = BookSerializer


# class Book4DetailView(UpdateAPIView, RetrieveAPIView, DestroyAPIView):  # 相当于继承了 RetrieveUpdateDestroyAPIView
class Book4DetailView(RetrieveUpdateDestroyAPIView):
    queryset = Book.objects
    serializer_class = BookSerializer


# 使用ModelViewSet 编写5个接口
from rest_framework.viewsets import ModelViewSet


class Book5View(ModelViewSet):  # 5个接口都有， 但是路由有问题
    queryset = Book.objects
    serializer_class = BookSerializer


from rest_framework.viewsets import ViewSetMixin, ViewSet


class Book6View(ViewSetMixin, APIView):
    def get_all_book(self, request):
        book_list = Book.objects.all()
        book_ser = BookSerializer(instance=book_list, many=True)
        return Response(book_ser.data)